Cubehelix Color Maps in Palettable

Cubehelix was designed by D.A. Green to provide a color mapping that would degrade gracefully to grayscale without losing information. This quality makes Cubehelix very useful for continuous colour scales in scientific visualizations that might be printed in grayscale at some point. James Davenport popularized Cubehelix among matplotlib users, and now this iteration brings Cubehelix to Palettable's beautiful API for managing color maps.


In [1]:
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

The Cubehelix functionality is contained within the palettable.cubehelix name space, and shares the same API as palettable.tableau, palettable.brewer.sequential, palettable.qualitative, palettable.diverging, and palettable.wesandersen, for instance.


In [2]:
from palettable import cubehelix

Making your own Cubehelix Maps

Unlike other color maps, Cubehelix is entirely algorithmic. Thus you can make your own map by instantiating a Cubehelix object with the Cubehelix.make() class method. For example, to make a color palette with 5 colors:


In [3]:
cube = cubehelix.Cubehelix.make(n=5, start_hue=240., end_hue=-300.,min_sat=1., max_sat=2.5,
                                min_light=0.3, max_light=0.8, gamma=.9)
cube.show_as_blocks()  # requires ipythonblocks


The Cubehelix docstring provides some guidance on how to build custom Cubehelix maps.


In [4]:
print(cubehelix.Cubehelix.make.__doc__)


        Create an arbitrary Cubehelix color palette from the algorithm.

        See http://adsabs.harvard.edu/abs/2011arXiv1108.5083G for a technical
        explanation of the algorithm.

        Parameters
        ----------
        start : scalar, optional
            Sets the starting position in the RGB color space. 0=blue, 1=red,
            2=green. Default is ``0.5`` (purple).
        rotation : scalar, optional
            The number of rotations through the rainbow. Can be positive
            or negative, indicating direction of rainbow. Negative values
            correspond to Blue->Red direction. Default is ``-1.5``.
        start_hue : scalar, optional
            Sets the starting color, ranging from [-360, 360]. Combined with
            `end_hue`, this parameter overrides ``start`` and ``rotation``.
            This parameter is based on the D3 implementation by @mbostock.
            Default is ``None``.
        end_hue : scalar, optional
            Sets the ending color, ranging from [-360, 360]. Combined with
            `start_hue`, this parameter overrides ``start`` and ``rotation``.
            This parameter is based on the D3 implementation by @mbostock.
            Default is ``None``.
        gamma : scalar, optional
            The gamma correction for intensity. Values of ``gamma < 1``
            emphasize low intensities while ``gamma > 1`` emphasises high
            intensities. Default is ``1.0``.
        sat : scalar, optional
            The uniform saturation intensity factor. ``sat=0`` produces
            grayscale, while ``sat=1`` retains the full saturation. Setting
            ``sat>1`` oversaturates the color map, at the risk of clipping
            the color scale. Note that ``sat`` overrides both ``min_stat``
            and ``max_sat`` if set.
        min_sat : scalar, optional
            Saturation at the minimum level. Default is ``1.2``.
        max_sat : scalar, optional
            Satuation at the maximum level. Default is ``1.2``.
        min_light : scalar, optional
            Minimum lightness value. Default is ``0``.
        max_light : scalar, optional
            Maximum lightness value. Default is ``1``.
        n : scalar, optional
            Number of discrete rendered colors. Default is ``256``.
        reverse : bool, optional
            Set to ``True`` to reverse the color map. Will go from black to
            white. Good for density plots where shade -> density.
            Default is ``False``.
        name : str, optional
            Name of the color map (defaults to ``'custom_cubehelix'``).

        Returns
        -------
        palette : `Cubehelix`
            A Cubehelix color palette.
        

Pre-made Cubehelix Maps

Of course, you can also use a number of pre-made color maps. The pre-made cubehelix maps have 16 computed colors. Since matplotlib's color maps are linearly interpolated, these work great as continuous color maps.

Like the other color maps in Palettable, the cubehelix maps are available from the palettable.cubehelix namespace. Here is a listing of color map names:


In [5]:
cubehelix.print_maps()


classic_16               sequential      
perceptual_rainbow_16    sequential      
purple_16                sequential      
jim_special_16           sequential      
red_16                   sequential      
cubehelix1_16            sequential      
cubehelix2_16            sequential      
cubehelix3_16            sequential      

And examples:


In [6]:
cubehelix.classic_16.show_continuous_image()



In [7]:
cubehelix.perceptual_rainbow_16.show_continuous_image()



In [8]:
cubehelix.purple_16.show_continuous_image()



In [9]:
cubehelix.jim_special_16.show_continuous_image()



In [10]:
cubehelix.red_16.show_continuous_image()



In [11]:
cubehelix.cubehelix1_16.show_continuous_image()



In [12]:
cubehelix.cubehelix2_16.show_continuous_image()



In [13]:
cubehelix.cubehelix3_16.show_continuous_image()


Of course, you can can reverse any color map by appending _r to the map's name.


In [14]:
cubehelix.cubehelix3_16_r.show_continuous_image()